// pages/song/song.js
import moment from 'moment';
import request from '../../utils/request';
import PubSub from 'pubsub-js'
let appInstance = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isPlay: false,
    songId: '',
    music: {},
    duration: '',
    currentTime: '00:00',
    musicLink: '',
    currentWith: 0
  },
  playMusic() {

    let {
      isPlay,
      songId,
      musicLink
    } = this.data
    isPlay = !isPlay
    this.setData({
      isPlay
    })
    this.controlMusic(isPlay, songId, musicLink)
  },

  async controlMusic(isPlay, id, musicLink) {
    if (!isPlay) {
      //暂停
      this.BackgroundAudioManager.pause()
      return;
    }
    if (!musicLink) {
      let res = await request('song/url', {
        id
      })
      // console.log(res.data[0].url);
      musicLink = res.data[0].url
      this.setData({
        musicLink
      })
    }

    if (!musicLink) {
      wx.showToast({
        title: '充钱可以让你听',
        icon: 'none'
      })
      return
    }

    this.BackgroundAudioManager.src = musicLink
    this.BackgroundAudioManager.title = this.data.music.name

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    // console.log(options);
    this.setData({
      songId: options.songId
    })
    this.getMusic(this.data.songId)
    this.BackgroundAudioManager = wx.getBackgroundAudioManager()
    if (appInstance.globalData.playStatus && appInstance.globalData.musicId == this.data.songId) {
      this.setData({
        isPlay: true
      })
    }
    this.BackgroundAudioManager.onPlay(() => {
      this.changeIsplay(true)
      appInstance.globalData.musicId = this.data.songId
    });
    this.BackgroundAudioManager.onPause(() => {
      this.changeIsplay(false)
    });
    this.BackgroundAudioManager.onStop(() => {
      this.changeIsplay(false)
    });
    this.BackgroundAudioManager.onTimeUpdate(() => {
      this.setData({
        currentTime: moment(this.BackgroundAudioManager.currentTime * 1000).format('mm:ss'),
        currentWith: this.BackgroundAudioManager.currentTime * 450 / this.BackgroundAudioManager.duration
      })
    })

    this.BackgroundAudioManager.onEnded(() => {
      this.setData({
        currentTime: '00:00',
        currentWith: 0
      })
      this.handleSwitch({currentTarget:{id:'next'}})
    })


  },
  tapHandle(event) {
    console.log(event);
  },
  changeIsplay(isPlay) {
    this.setData({
      isPlay
    })
    appInstance.globalData.playStatus = isPlay
  },
  async getMusic(ids) {
    let res = await request('song/detail', {
      ids
    });

    this.setData({
      music: res.songs[0],
      duration: moment(res.songs[0].dt).format('mm:ss')
    })

    wx.setNavigationBarTitle({
      title: res.songs[0].name,
    })
  },
  handleSwitch(event){
    PubSub.subscribe('musicId',(msg,songId)=>{
      this.setData({songId})
      this.getMusic(songId)
      this.controlMusic(true, songId)
      PubSub.unsubscribe('musicId');
    })
    let type = event.currentTarget.id;
    PubSub.publish('switchType', type);
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})